package com.ruoyi.system.task;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;
@Component
public class Myscheduler {
       static Scheduler scheduler = null;
       static JobDetail jobDetail = null;
       static Trigger trigger = null;
       /*static Date endDate = new Date();
       static Date startDate = new Date();*/
    // 1、创建调度器Scheduler

        public void GenMyscheduler() throws SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        scheduler = schedulerFactory.getScheduler();
        }

    // 2、创建JobDetail实例，并与TerminationJob类绑定(Job执行内容)
        public void genJob(){
            jobDetail = JobBuilder.newJob(TerminationJob.class).withIdentity("job3", "group3").build();
            // 3、构建CronTrigger实例,指定时间
            trigger = TriggerBuilder.newTrigger().withIdentity("trigger3", "triggerGroup3")
                    .usingJobData("trigger3", "这是jobDetail3的trigger")
                    .startNow()//立即生效
                    /*.startAt(startDate)
                    .endAt(endDate)*/
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                            .withIntervalInSeconds(1)//每隔1s执行一次
                            .repeatForever()).build();//一直执行
        }

        public void ScheduleJobStart(){
            //4、执行
        try {
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        System.out.println("--------scheduler start ! ------------");
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

        public void ScheduleJobSleep(){
            //睡眠
        try {
            scheduler.shutdown();
            System.out.println("--------scheduler shutdown ! ------------");
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

        }

    }


